package com.bianmaba.spring.security.util;

import com.bianmaba.spring.security.basic.handler.AjaxGlobalExceptionHandler;
import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: bianmaba-web-framework-extends
 * @description:
 * @author: Chenjiabin
 * @create: 2018-04-22 20:29
 **/
public final class ResponseUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(ResponseUtil.class);

    private ResponseUtil() {
    }

    public static void writeJson(HttpServletResponse response, Object data, Integer status) throws IOException {
        String json = null;
        try {
            response.setStatus(status);
            response.setContentType("application/x-json;charset=UTF-8");
            json = JSONUtil.serialize(data);
        } catch (JSONException e) {
            LOGGER.error("JSON对象序列化出现异常。", e);
        }
        response.getOutputStream().write(json.getBytes("UTF-8"));
        response.getOutputStream().flush();
    }
}
